<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable size="small" @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">搜索</el-button>

          <el-button v-if="permissions.product_category_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle(false)">
            添加
          </el-button>
        </el-form-item>
      </el-form>

      <el-table border v-loading="loading" :data="menuList" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildrens'}">
        <el-table-column prop="name" label="分类名称" :show-overflow-tooltip="true" width="180"></el-table-column>
        <el-table-column prop="imgUrl" label="分类的显示图片" align="center" width="180">
          <template slot-scope="scope">
            <el-image v-if="scope.row.imgUrl" :src="scope.row.imgUrl" :preview-src-list="[scope.row.imgUrl]" fit="fill" style="width: 50px; height: 50px" />
          </template>
        </el-table-column>
        <el-table-column prop="icon" label="图标" align="center" width="100">
          <template slot-scope="scope">
            <el-image v-if="scope.row.icon" :src="scope.row.icon" :preview-src-list="[scope.row.icon]" fit="fill" style="width: 50px; height: 50px" />
          </template>
        </el-table-column>
        <el-table-column prop="shopId" label="店铺" width="60">
          <template slot-scope="scope">
            {{
              scope.row.shopId && shopDict ? shopDict[scope.row[$attrs.prop]] : scope.row[$attrs.prop]
            }}
          </template>
        </el-table-column>
        <el-table-column prop="categoryLevel" label="层级" width="60"></el-table-column>
        <el-table-column prop="sortOrder" label="排序" width="60"></el-table-column>
        <el-table-column prop="status" label="状态" width="80" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status === 0">禁用</el-tag>
            <el-tag type="success" v-if="scope.row.status === 1">开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-plus" @click="addOrUpdateHandle(false,scope.row.id,scope.row.categoryLevel)" v-if="permissions.product_category_add && scope.row.categoryLevel<3">新增</el-button>
            <el-button size="small" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(true,scope.row.id,scope.row.categoryLevel)" v-if="permissions.product_category_edit">修改</el-button>
            <el-button size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-if="permissions.product_category_del">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></table-form>
    </div>
  </basic-container>
</template>

<script>
import { fetchTree, delObj } from '@/api/product/category'
import { list } from '@/api/product/shopDetail'
import TableForm from './category-form'
import { mapGetters } from 'vuex'

export default {
  name: 'category',
  components: { TableForm },
  data() {
    return {
      addOrUpdateVisible: false,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 查询参数
      queryParams: {
        parentId: undefined,
        name: ""
      },
      shopDict: null,
    };
  },
  mounted() {
    list().then(res => {
      res.data.data.reduce((pv, cv) => {
        pv[cv.shopId] = cv.shopName;
        return pv;
      }, (this.shopDict = {}));
    })
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(['permissions']),
  },
  methods: {
    addOrUpdateHandle(isEdit, id, categoryLevel) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        if (id === undefined) categoryLevel = 0
        this.$refs.addOrUpdate.init(isEdit, id, isEdit ? categoryLevel : categoryLevel + 1)
      })
    },
    getList() {
      this.loading = true;
      fetchTree(this.queryParams).then(response => {
        this.menuList = response.data.data
        this.loading = false;
      });
    },
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delObj(row.id);
      }).then(() => {
        this.getList();
        this.$message.success('删除成功')
      })
    }
  }
}
</script>
